package live.yanxiaohui.transaction.consumer;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import live.yanxiaohui.transaction.entity.Player;
import live.yanxiaohui.transaction.entity.SysUser;
import live.yanxiaohui.transaction.mapper.PlayerMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description todo
 * @CSDN https://blog.csdn.net/yxh13521338301
 * @Author： yanxh<br>
 * @Date 2020-06-29 11:21<br>
 * @Version 1.0<br>
 */
@Component

public class PlayerConsumer {

    @Autowired
    private PlayerMapper playerMapper;

    @RabbitListener(queues = "player_queue")
    public void consumer(Message message, Channel channel) throws Exception{
        String json = new String(message.getBody());
        SysUser user = JSONObject.parseObject(json, SysUser.class);
        Player player = new Player();
        player.setUserId(user.getMsgId());
        player.setName("玩家1号");
        int result = playerMapper.addPlay(player);
        if(result<0){
            System.out.println("消息消费失败");
        }
        System.out.println("消息消费成功");
        // 手动ack，通知mq删除消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
